Wiki  - Library Entry

Iterator

GoF

Behavioral

Intent:

Provide a way to access the elements of a aggregate object without exposing its underlying representation.

UML

Applicability:

 Solution:

1.     Create an Iterator class interface which defines a basic, uniform set of functions for traversing and accessing elements. Create an Aggregate class interface for maintainance of an aggregate structure and creation of Iterator(s) for traversal.

2.     Define the SpecificAggregate classes to implement an Aggregate structure of specific Elements.

3.     For each method of traversal needed, create a ConcreteIterator which implements the Iterator interface to traverse and access elements encapsulated by a SpecificAggregate class.

4.     Create the Element class for objects which will be encapsulated by Aggregate and returned by the traversal functions implemented by the ConcreteIterator(s).

5.     Note: The Iterator and Aggregate class interfaces are typically defined in C++ using class templates, for example, see the C++ STL library.

 Consequences: